package com.ry.est.service.impl;

import com.ry.est.constants.Constants;
import com.ry.est.dao.MalfunctionDao;
import com.ry.est.dto.MalfunctionDTO;
import com.ry.est.dto.MalfunctionOptionsDTO;
import com.ry.est.entity.Malfunction;
import com.ry.est.service.MalfunctionOptionsService;
import com.ry.est.service.MalfunctionService;
import com.ry.est.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ry
 * @since 2021-11-09 08:59
 **/
@Service
public class MalfunctionServiceImpl implements MalfunctionService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private MalfunctionDao malfunctionDao;

    @Autowired
    private MalfunctionOptionsService malfunctionOptionsService;

    @Override
    public List<MalfunctionDTO> listByModelId(Long modelId) {
        List<Malfunction> malfunctions = malfunctionDao
                .listByModelId(modelId, Constants.EST_NOT);
        List<MalfunctionDTO> malfunctionDTOS = BeanUtils
                .convertToDTOList(malfunctions, MalfunctionDTO.class);

        for (MalfunctionDTO malfunctionDTO : malfunctionDTOS) {
            Long id = malfunctionDTO.getId();
            List<MalfunctionOptionsDTO> malfunctionOptionsDTOS = malfunctionOptionsService.listByMalfId(id);
            malfunctionDTO.setOptionsList(malfunctionOptionsDTOS);
        }

        return malfunctionDTOS;
    }

    @Override
    public MalfunctionDTO getById(Long id) {
        Malfunction malfunction = malfunctionDao.selectByPrimaryKey(id);
        MalfunctionDTO malfunctionDTO = BeanUtils.convertToDTO(malfunction, MalfunctionDTO.class);

        return malfunctionDTO;
    }
}
